﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using System.Globalization;
using System.IO;

namespace Managed_Metadata_Loader
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();

        }


  

        private void btnGetTermStores_Click(object sender, EventArgs e)
        {
            try
            {            
                txtStatus.Text = "Retrieving Term Stores...";
                txtStatus.Update();
                
                var currentSite = new SPSite("http://localhost");
                TaxonomySession session = new TaxonomySession(currentSite);
                TermStoreCollection termstores = session.TermStores;


                listBox1.Items.Clear();
                foreach (TermStore store in termstores)
                {
                    listBox1.Items.Add(store.Name.ToString());
                }

                txtStatus.Text = "Retrieved " + session.TermStores.Count.ToString() + " Term Stores";

            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Retrieving TermStores: " + e2.Message;
            }
        }

        private void btnGetGroups_Click(object sender, EventArgs e)
        {
            try
            {
                txtStatus.Text = "Retrieving Groups...";
                txtStatus.Update();

                var currentSite = new SPSite("http://localhost/");
                TaxonomySession session = new TaxonomySession(currentSite);
                TermStore termStore = session.TermStores[listBox1.SelectedItem.ToString()];

                listBox2.Items.Clear();
                foreach (Group g in termStore.Groups)
                {
                    listBox2.Items.Add(g.Name.ToString());
                }

                txtStatus.Text = "Retrieved " + termStore.Groups.Count.ToString() + " Groups";
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Retrieving Groups: " + e2.Message;
            }   
        }

        private void btnGetTermSets_Click(object sender, EventArgs e)
        {
            try
            {
                txtStatus.Text = "Retrieving TermSets...";
                txtStatus.Update();

                var currentSite = new SPSite("http://localhost/");
                TaxonomySession session = new TaxonomySession(currentSite);
                TermStore termStore = session.TermStores[listBox1.SelectedItem.ToString()];
                Group storeGroup = termStore.Groups[listBox2.SelectedItem.ToString()];

                listBox3.Items.Clear();
                foreach (TermSet ts in storeGroup.TermSets)
                {
                    listBox3.Items.Add(ts.Name.ToString());
                }

                txtStatus.Text = "Retrieved " + storeGroup.TermSets.Count.ToString() + " TermSets";

                
                    btnAddTerms.Enabled = false;
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Retrieving Term Sets: " + e2.Message;
            } 
        }

        public void btnGetTerms_Click(object sender, EventArgs e)
        {
            try
            {
                txtStatus.Text = "Retrieving Terms...";
                txtStatus.Update();

                var currentSite = new SPSite("http://localhost/");
                TaxonomySession session = new TaxonomySession(currentSite);
                TermStore termStore = session.TermStores[listBox1.SelectedItem.ToString()];
                Group storeGroup = termStore.Groups[listBox2.SelectedItem.ToString()];
                TermSet termSet = storeGroup.TermSets[listBox3.SelectedItem.ToString()];

                listBox4.Items.Clear();
                foreach (Term t in termSet.Terms)
                {
                    listBox4.Items.Add(t.Name.ToString());
                }

                txtStatus.Text = "Retrieved " + termSet.Terms.Count.ToString() + " Terms";
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Retrieving Terms: " + e2.Message;
            } 
        }

        private void btnAddTerms_Click(object sender, EventArgs e)
        {
            try
            {
                txtStatus.Text = "Adding Terms...";
                txtStatus.Update();

                var currentSite = new SPSite("http://localhost/");
                TaxonomySession session = new TaxonomySession(currentSite);
                TermStore termStore = session.TermStores[listBox1.SelectedItem.ToString()];
                Group storeGroup = termStore.Groups[listBox2.SelectedItem.ToString()];
                TermSet termSet = storeGroup.TermSets[listBox3.SelectedItem.ToString()];


                using (SPWeb web = currentSite.RootWeb)
                {
                    int lcid = (int)web.Language;
                    CultureInfo defaultCulture = new CultureInfo(lcid);

                    termSet.CreateTerm(txtNewTerm.Text.ToString(), defaultCulture.LCID);
                    termStore.CommitAll();
                }

                btnGetTerms_Click("Add",e);
                txtStatus.Text = "Added Term: " + txtNewTerm.Text.ToString();
                txtNewTerm.Text = "";
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Adding Terms: " + e2.Message;
            } 
        }

        private void btnDeleteTerm_Click(object sender, EventArgs e)
        {
            try
            {
                txtStatus.Text = "Deleting Term...";
                txtStatus.Update();

                var currentSite = new SPSite("http://localhost/");
                TaxonomySession session = new TaxonomySession(currentSite);
                TermStore termStore = session.TermStores[listBox1.SelectedItem.ToString()];
                Group storeGroup = termStore.Groups[listBox2.SelectedItem.ToString()];
                TermSet termSet = storeGroup.TermSets[listBox3.SelectedItem.ToString()];
                Term term = termSet.Terms[listBox4.SelectedItem.ToString()];
                var TermName = term.Name.ToString();

                term.Delete();
                termStore.CommitAll();

                btnGetTerms_Click("Delete", e);
                txtStatus.Text = "Deleted Term: " + TermName;

                btnDeleteTerm.Enabled = false;
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Deleting Term " + e2.Message;
            } 
        }

      
   
        private void btnAddCSV_Click(object sender, EventArgs e)
        {
            try
            {
                txtStatus.Text = "Adding Terms...";
                txtStatus.Update();

                var currentSite = new SPSite("http://localhost/");
                TaxonomySession session = new TaxonomySession(currentSite);
                TermStore termStore = session.TermStores[listBox1.SelectedItem.ToString()];
                Group storeGroup = termStore.Groups[listBox2.SelectedItem.ToString()];
                TermSet termSet = storeGroup.TermSets[listBox3.SelectedItem.ToString()];


                using (SPWeb web = currentSite.RootWeb)
                {
                    int lcid = (int)web.Language;
                    CultureInfo defaultCulture = new CultureInfo(lcid);


                    foreach (string s in listBox5.Items)
                    {
                        try
                        {
                            termSet.CreateTerm(s.ToString(), defaultCulture.LCID);
                            termStore.CommitAll();
                        }
                        catch (Exception ex)
                        {
                            txtStatus.Text = "Error Adding CSV Term to Termset: " + ex.Message;
                        }   
                    }
                }

                btnGetTerms_Click("Add", e);
                txtStatus.Text = "Added CSV Terms to Termset: " + listBox3.SelectedItem.ToString();
                txtNewTerm.Text = "";
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Adding CSV to Termset: " + e2.Message;
            }
        }


        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex >= 0)
            {
                btnDeleteTerm.Enabled = true;
            }
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex >= 0)
            {
                btnGetTerms.Enabled = true;
            }
            else
            {
                btnGetTerms.Enabled = false;
            }
            
            if(txtNewTerm.Text != "")
            {
                btnAddTerms.Enabled = true;
            }

            listBox5.Items.Clear();
            btnAddCSV.Enabled = false;
            
            if (txtCSVFilePath.Text != "")
            {
                btnCSV.Enabled = true;
            }

            listBox4.Items.Clear();
            btnDeleteTerm.Enabled = false;
        }

               

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnBrowseFile_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "Comma Seperated Value |*.csv";
                openFileDialog1.FileName = "";

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    txtCSVFilePath.Text = openFileDialog1.FileName;
                }
                
                
                if (txtCSVFilePath.Text != "" && listBox3.SelectedIndex >= 0)
                {
                    btnCSV.Enabled = true;
                }

                listBox5.Items.Clear();
                btnAddCSV.Enabled = false;
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Browsing for CSV: " + e2.Message;
            }
            
            
        }

        private void btnCSV_Click(object sender, EventArgs e)
        {
            try
            {
                using (var rd = new StreamReader(@txtCSVFilePath.Text))
                {
                    var i = 0;

                    listBox5.Items.Clear();
                    
                    while (!rd.EndOfStream)
                    {
                        if (i == 1)
                        {
                            var splits = rd.ReadLine().Split(',');
                            
                            if (splits[0] == listBox3.SelectedItem.ToString())
                            {
                                listBox5.Items.Add(splits[5]);
                            }
                        }
                        else
                        {
                            var splits = rd.ReadLine().Split(',');
                            i = 1;
                        }

                    }
                }
                
                if (listBox5.Items.Count > 0)
                {
                    btnAddCSV.Enabled = true;
                }
                else
                {
                    btnAddCSV.Enabled = false;
                }
            }
            catch (Exception e2)
            {
                txtStatus.Text = "Error Processing CVS File: " + e2.Message;
            }
        }

        private void txtNewTerm_TextChanged(object sender, EventArgs e)
        {
            if (txtNewTerm.Text != "" && listBox4.SelectedIndex > 0)
            {
                btnAddTerms.Enabled = true;
            } else {
                btnAddTerms.Enabled = false;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                btnGetGroups.Enabled = true;
            } else {
                btnGetGroups.Enabled = false;
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex >= 0)
            {
                btnGetTermSets.Enabled = true;
            }
            else
            {
                btnGetTermSets.Enabled = false;
            }

            listBox3.Items.Clear();
            listBox4.Items.Clear();
            btnGetTerms.Enabled = false;
            btnDeleteTerm.Enabled = false;
        }


 


    }
}
